layui.use(['laytpl','jquery','laydate','layer','form'],function(layptl,$,laydate,layer,form){
    var schoolId = null;
    var semesterID = null;

    var data = null;
    var data2 = null;
    var data3 = null;
    window.onload = function () {

        $.post({
            url:"/school/getAllSchoolName",
            async:false,
            success: function (res) {
                // console.log(res);
                data3 = res;
            }
        });

        // var laytpl = layui.laytpl;
        //建立视图。用于呈现渲染结果。
        var gettp3 = document.getElementById('testTp3').innerHTML;
        layptl(gettp3).render(data3, function (html) {
            // console.log(html)
            //得到的模板渲染到html
            document.getElementById('test1').innerHTML = html;
        });

        $.post({
            url:"/sys/querySemesterIdDistinct",
            async:false,
            success: function (res) {
                // console.log(res);
                data2 = res;
            }
        });

        // var laytpl = layui.laytpl;
        //建立视图。用于呈现渲染结果。
        var gettp2 = document.getElementById('testTp2').innerHTML;
        layptl(gettp2).render(data2, function (html) {
            // console.log(html)
            //得到的模板渲染到html
            document.getElementById('test2').innerHTML = html;
        });

        // var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        form.render();
        form.render('select' ,'test'); //刷新select选择框渲染

        $.post({
            url:"/sys/queryDayTime?schoolId=" + schoolId + "&semesterID=" + semesterID,
            async:false,
            success: function (res) {
                // console.log(res);
                data = res;
            }
        });

        // var laytpl = layui.laytpl;
        //建立视图。用于呈现渲染结果。
        var gettpl = document.getElementById('testTpl').innerHTML;
        layptl(gettpl).render(data, function (html) {
            // console.log(html)
            //得到的模板渲染到html
            document.getElementById('course').innerHTML = html;
        });

        form.on('select(mySelect1)', function (data) {
            schoolId = data.value;
            if(semesterID != null) {
                $.post({
                    url:"/sys/queryDayTime?schoolId=" + schoolId + "&semesterID=" + semesterID,
                    async:false,
                    success: function (res) {
                        // console.log(res);
                        data = res;
                    }
                });

                layptl(gettpl).render(data, function (html) {
                    // console.log(html)
                    //得到的模板渲染到html
                    document.getElementById('course').innerHTML = html;
                });

                lay('.test-item').each(function(data) {
                    laydate.render({
                        elem: this
                        ,type: 'time'
                        ,range: true
                        ,format: 'HH:mm'	//日期格式化
                        // ,value: '08:00 - 08:45'
                        ,isInitValue: true
                        ,done: function(value1){
                            var times = value1.split(' - ');
                            var i = data + 1;
                            // localhost:80/sys/setCourseTime?schoolId=203&semesterID=2019-2020-2&course=5&stTime=08:00&edTime=12:00
                            $.post({
                                url:"/sys/setCourseTime?schoolId=" + schoolId + "&semesterID=" + semesterID + "&course=" + i + "&stTime=" + times[0] + "&edTime=" + times[1] ,
                                async:false,
                                success: function (res) {
                                    // console.log(res);
                                    if(res == 1) {
                                        layer.msg('修改成功',{icon:7,time:800,shade:[0.6,'#000',true]});
                                    }else {
                                        // layer.alert('您设置的时间区间有误，修改失败',300)
                                        top.layer.msg('您设置的时间区间有误，修改失败',{icon:7,time:800,shade:[0.6,'#000',true]});
                                    }
                                }
                            });
                        }
                    });

                });

            }
        });

        form.on('select(mySelect2)', function (data) {
            semesterID = data.value;
            if(schoolId != null) {
                $.post({
                    url:"/sys/queryDayTime?schoolId=" + schoolId + "&semesterID=" + semesterID,
                    async:false,
                    success: function (res) {
                        // console.log(res);
                        data = res;
                    }
                });

                layptl(gettpl).render(data, function (html) {
                    // console.log(html)
                    //得到的模板渲染到html
                    document.getElementById('course').innerHTML = html;
                });

                lay('.test-item').each(function(data) {
                    laydate.render({
                        elem: this
                        ,type: 'time'
                        ,range: true
                        ,format: 'HH:mm'	//日期格式化
                        // ,value: '08:00 - 08:45'
                        ,isInitValue: true
                        ,done: function(value1){
                            var times = value1.split(' - ');
                            var i = data + 1;
                            // localhost:80/sys/setCourseTime?schoolId=203&semesterID=2019-2020-2&course=5&stTime=08:00&edTime=12:00
                            $.post({
                                url:"/sys/setCourseTime?schoolId=" + schoolId + "&semesterID=" + semesterID + "&course=" + i + "&stTime=" + times[0] + "&edTime=" + times[1] ,
                                async:false,
                                success: function (res) {
                                    // console.log(res);
                                    if(res == 1) {
                                        layer.msg('修改成功',{icon:7,time:800,shade:[0.6,'#000',true]});
                                    }else {
                                        // layer.alert('您设置的时间区间有误，修改失败',300)
                                        top.layer.msg('您设置的时间区间有误，修改失败',{icon:7,time:800,shade:[0.6,'#000',true]});
                                    }
                                }
                            });
                        }
                    });

                });

            }
        });

        lay('.test-item').each(function(data) {
             laydate.render({
                elem: this
                ,type: 'time'
                ,range: true
                ,format: 'HH:mm'	//日期格式化
                // ,value: '08:00 - 08:45'
                ,isInitValue: true
                ,done: function(value1){
                    var times = value1.split(' - ');
                    var i = data + 1;
                    // localhost:80/sys/setCourseTime?schoolId=203&semesterID=2019-2020-2&course=5&stTime=08:00&edTime=12:00
                    $.post({
                        url:"/sys/setCourseTime?schoolId=" + schoolId + "&semesterID=" + semesterID + "&course=" + i + "&stTime=" + times[0] + "&edTime=" + times[1] ,
                        async:false,
                        success: function (res) {
                            // console.log(res);
                            if(res == 1) {
                                layer.msg('修改成功',{icon:7,time:800,shade:[0.6,'#000',true]});
                            }else if(res == 2) {
                                top.layer.msg('请先选择学校和学期！',{icon:7,time:800,shade:[0.6,'#000',true]});
                            } else {
                                // layer.alert('您设置的时间区间有误，修改失败',300)
                                top.layer.msg('您设置的时间区间有误，修改失败',{icon:7,time:800,shade:[0.6,'#000',true]});
                            }
                        }
                    });
                }
            });

        });

    }
});
